package com.example.tables.controller;

import com.alibaba.excel.EasyExcel;
import com.example.tables.bean.TableABean;
import com.example.tables.bean.TableReadListener;
import com.example.tables.service.TableAService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tablea")
public class TableAController {
    @Autowired
    TableAService tableAService;

    // add new data
    @RequestMapping("/addData")
    public Object addData(@RequestParam(value = "a")String a,
                          @RequestParam(value = "b")String b,
                          @RequestParam(value = "c")String c,
                          @RequestParam(value = "d")String d,
                          @RequestParam(value = "e")String e,
                          @RequestParam(value = "f")String f,
                          @RequestParam(value = "aa")int aa,
                          @RequestParam(value = "bb")int bb,
                          @RequestParam(value = "cc")int cc,
                          @RequestParam(value = "dd")int dd,
                          @RequestParam(value = "ee")int ee){
        Map<String,Object> map =new HashMap<>();
        TableABean tableABean = new TableABean();
        tableABean.setA(a);
        tableABean.setB(b);
        tableABean.setC(c);
        tableABean.setD(d);
        tableABean.setE(e);
        tableABean.setF(f);
        tableABean.setAa(aa);
        tableABean.setBb(bb);
        tableABean.setCc(cc);
        tableABean.setDd(dd);
        tableABean.setEe(ee);

        int check =tableAService.addData(tableABean);
        if(check==1){
            map.put("code",200);
            map.put("msg", "success!");
        }else{
            map.put("code",100);
            map.put("msg", "fail!");
        }

        return map;
    }


    //update data
    @RequestMapping("/update")
    public Object update(@RequestParam(value = "a")String a,
                         @RequestParam(value = "b")String b,
                         @RequestParam(value = "c")String c,
                         @RequestParam(value = "d")String d,
                         @RequestParam(value = "e")String e,
                         @RequestParam(value = "f")String f,
                         @RequestParam(value = "aa")int aa,
                         @RequestParam(value = "bb")int bb,
                         @RequestParam(value = "cc")int cc,
                         @RequestParam(value = "dd")int dd,
                         @RequestParam(value = "ee")int ee){
        Map<String,Object> map =new HashMap<>();

        int check =tableAService.update( a,  b,  c,  d,  e,  f,  aa,  bb,  cc,  dd,  ee);
        if(check==1){
            map.put("code",200);
            map.put("msg", "success!");
        }else{
            map.put("code",100);
            map.put("msg", "fail!");
        }
        return map;
    }
    @RequestMapping("/delete")
    public Object delete(@RequestParam(value = "a")String a){
        Map<String,Object> map =new HashMap<>();
        int check =tableAService.delete(a);
        if(check==1){
            map.put("code",200);
            map.put("msg", "success!");
        }else{
            map.put("code",100);
            map.put("msg", "fail!");
        }
        return map;

    }

    @RequestMapping("/get")
    public Object getLike(@RequestParam(value = "a")String a){
        Map<String,Object> map =new HashMap<>();
        List<TableABean> data = tableAService.getData(a);
        int i = data.size();
        if(data!= null || data.size()<0){
            map.put("code",200);
            map.put("msg", "success!");
            map.put("size", i);
            map.put("data", data);
        }else{
            map.put("code",100);
            map.put("msg", "fail!");
        }

        return map;
    }

    @RequestMapping("/import")
    public void importExcel() throws FileNotFoundException {
        File file = new File("C:/tablea.xlsx");
        InputStream is = new FileInputStream(file);

        TableReadListener listener = new TableReadListener(tableAService);

        EasyExcel.read(is,
                TableABean.class,
                listener).sheet(0)
                .headRowNumber(1)
                .doRead();
    }


}
